package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author 花鸟の虫鱼
 * @CreateDate 21/9/4 14:02
 * @Version 1.0
 * @Description TODO
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

//    进货入库，供应商下拉查询
    @Override
    public List<Supplier> comboboxList(String q) {
        return supplierDao.getComboboxList(q);
    }

    //  分页显示
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {

        Map<String,Object> map = new HashMap<>();
        //
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.getSupplierList(offSet, rows, supplierName);
        map.put("total", supplierDao.getSupplierCount(supplierName));
        map.put("rows", supplierList);
        return map;

    }

    //  新增或更新
   @Override
    public ServiceVO save(Supplier supplier) {
        //  供应商id为空时，说明是新增操作，需要先判断供应商是否存在
        if (supplier.getSupplierId() == null) {
            //  id为空执行新增方法
            supplierDao.insertSupplier(supplier);
        } else {
            //  id不为空执行更新方法
            supplierDao.updateSupplier(supplier);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    //  删除
    @Override
    public ServiceVO delete(String  ids) {
        String[] idsList = ids.split(",");
        supplierDao.deleteSupplier(idsList);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }



}
